package com.windfone.dao.impl.jdbc.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.windfone.dao.AuthorityDao;
import com.windfone.model.OperateAuthority;

public class AuthorityDaoImpl implements AuthorityDao {
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 
		this.jdbcTemplate = jdbcTemplate;
	}

	String DELETE_SQL = " DELETE FROM  operate_authority WHERE id=? ";
	public int deleteOperateAuthority(final int keyValue) {
		int rtv = 0;
		try {
			rtv = jdbcTemplate.update(
				DELETE_SQL,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)	throws SQLException {
						ps.setInt(1, keyValue);
					}
				}
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtv;
	}
 
	String UPDATE_SQL = " UPDATE operate_authority SET userId=? ,userName=? ,authrityDesc=?  WHERE id=? ";
	public int updateOperateAuthority(final OperateAuthority bean) {
		int rtv = 0;
		try {
			rtv = jdbcTemplate.update(
					UPDATE_SQL,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)	throws SQLException {
						int i=1;
						ps.setInt(i++,bean.getUserId());
						ps.setString(i++,bean.getUserName());
						ps.setString(i++,bean.getAuthrityDesc());
						ps.setInt(i++,bean.getId());
					}
				}
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtv;
	}

	String INSERT_SQL = " INSERT INTO operate_authority (userId,userName,authrityDesc )  VALUES ( ?,?,? ) ";
	public int insertOperateAuthority(final OperateAuthority bean) {
		int rtv = 0;
		try {
			rtv = jdbcTemplate.update(
					INSERT_SQL,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)	throws SQLException {
						int i=1;
						ps.setInt(i++,bean.getUserId());
						ps.setString(i++,bean.getUserName());
						ps.setString(i++,bean.getAuthrityDesc());
					}
				}
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtv;		
	}

	String GET_BYID_SQL = "  SELECT * FROM  operate_authority WHERE   id=? ";
	public List<OperateAuthority> getOperateAuthorityById(final int keyValue) {
		final List<OperateAuthority> result = new ArrayList<OperateAuthority>();
		try {
			jdbcTemplate.query(
				GET_BYID_SQL,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)	throws SQLException {
						ps.setInt(1, keyValue);
					}
				},
				new RowCallbackHandler() { 
					public void processRow(ResultSet rs) throws SQLException {
						OperateAuthority to= new OperateAuthority();
						to.setId(rs.getInt("id"));
						to.setUserName(rs.getString("userName"));
						to.setAuthrityDesc(rs.getString("authrityDesc"));
						to.setUserId(rs.getInt("userId"));
						result.add(to);
					}
				}
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	String GET_BY_USERID_SQL = "  SELECT * FROM  operate_authority WHERE   userId=? ";
	public List<OperateAuthority> getOperateAuthorityByUserId(final int userId) {
		final List<OperateAuthority> result = new ArrayList<OperateAuthority>();
		try {
			jdbcTemplate.query(
				GET_BY_USERID_SQL,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)	throws SQLException {
						ps.setInt(1, userId);
					}
				},
				new RowCallbackHandler() { 
					public void processRow(ResultSet rs) throws SQLException {
						OperateAuthority to= new OperateAuthority();
						to.setId(rs.getInt("id"));
						to.setUserName(rs.getString("userName"));
						to.setAuthrityDesc(rs.getString("authrityDesc"));
						to.setUserId(rs.getInt("userId"));
						result.add(to);
					}
				}
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
